package com.example.design.shejimoshi.action.strategy.注解with策略模式;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @date 2022/11/11 11:28
 * @Author liuzhihui
 * @Version 1.0
 */
@Component
public class OrderHandlerFactory {
    private Map<String, OrderHandler> orderHandlerMap;

    @Autowired
    public void setOrderHandlerMap(List<OrderHandler> orderHandlers) {
        // (v1, v2) -> v1 表示转map的时候，key重复，取v1
        orderHandlerMap = orderHandlers.stream().collect(Collectors.toMap(
                orderHandler -> AnnotationUtils.findAnnotation(orderHandler.getClass(), OrderHandlerType.class).source(),
                v -> v,
                (v1, v2) -> v1)
        );
    }

    public Map<String, OrderHandler> getOrderHandlerMap() {
        return orderHandlerMap;
    }
}